<template>
	<view>
		<view class="cu-bar search bg-white">
			<view class="search-form round" style="text-align: center;">
				<input @submit.prevent="search" v-model="searchTxt" :adjust-position="false" type="text" placeholder="微信号/手机号"
				 confirm-type="search"></input>
			</view>
		</view>

		<view class="cu-list menu-avatar" v-if="friend!=null">
			<view class="cu-item">
				<view class="cu-avatar round lg" :style="'background-image:url('+ friend.faceImage +');'"></view>
				<view class="content">
					<view class="text-grey">{{friend.username}}</view>
					<view class="text-gray text-sm flex">
						<view class="text-cut">{{friend.nickname}}</view>
					</view>
				</view>
				<view style="padding-right: 20upx;">
					<button class="cu-btn bg-blue" @tap="sendRequest">添加</button>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		created() {
			this.userId = this.$tool.getStorageSync("userInfo").id
		},
		data() {
			return {
				userId: null,
				searchTxt: null,
				friend: null
			}
		},
		methods: {
			async search() {
				this.friend = null
				var res = await this.$request({ // 发送异步请求
					baseURL: this.$tool.serverURL,
					url: 'front/friend/searchFriend',
					params: {
						myUserId: this.userId,
						friendUsername: this.searchTxt
					},
					type: 'get'
				})
				if (res.status == 200) {
					this.friend = res.data
				} else {
					uni.showToast({
						icon: 'none',
						title: res.msg
					})
				}
			},
			async sendRequest() {
				var res = await this.$request({ // 发送异步请求
					baseURL: this.$tool.serverURL,
					url: 'user/sendRequest',
					params: {
						myUserId: this.userId,
						username: this.searchTxt,
						requestMsg: '加一加我吧！'
					},
					type: 'get'
				})
				if (res.code == 200) {
					uni.showToast({
						icon:'none',
						title:'已发送好友请求，待对方验证',
						duration:1000
					})
				} else {
					uni.showToast({
						icon: 'none',
						title: res.msg
					})
				}
			}
		}
	}
</script>

<style>
</style>
